home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / xcdplayer / shuffle.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  133 lines

  1. /*
  2.  * Copyright (C) 1990 Regents of the University of California.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of the University of
  9.  * California not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  the University of California makes no representations
  12.  * about the suitability of this software for any purpose.  It is provided
  13.  * "as is" without express or implied warranty.
  14.  */
  15.  
  16. # include <X11/Intrinsic.h>
  17. # include <stdio.h>
  18.  
  19. # include "debug.h"
  20. # include "cdrom_globs.h"
  21. #ifdef sun
  22. # include "cdrom_sun.h"
  23. #endif
  24. #ifdef sgi
  25. # include "cdrom_sgi.h"
  26. #endif
  27.  
  28. # define ran(min, max)  ((random() % ((max) - (min))) + (min))
  29.  
  30. static unsigned char    *random_tracks;
  31.  
  32. void
  33. shuffle_setup() {
  34.     extern char    *malloc();
  35. #ifdef sgi
  36.     extern time_t    time(time_t *);
  37. #else
  38.     extern long    time();
  39. #endif
  40.     extern long    random();
  41.     unsigned long    seed, now;
  42.     char        state[128];
  43.     int        try;
  44.     int        i, j;
  45.  
  46. #if 0 
  47.     if (cdi.state & CDROM_STATE_EJECTED) {
  48.         return;
  49.     }
  50. #endif
  51.  
  52.     if (cdi.maxtrack == cdi.mintrack) {
  53.         cdi.currand = -1;
  54.         return;
  55.     }
  56.  
  57.     cdi.ntracks = (cdi.maxtrack - cdi.mintrack) + 1;
  58.  
  59.     cdi.currand = 0;
  60.  
  61.     if (random_tracks != NULL) {
  62.         free(random_tracks);
  63.         random_tracks = NULL;
  64.     }
  65.  
  66.     if ((random_tracks = (unsigned char *) malloc(cdi.ntracks)) == NULL) {
  67.         perror("malloc");
  68.         exit(1);
  69.     }
  70.  
  71.     now = time((long *) 0);
  72.     seed = now & 0xfff;
  73.  
  74.     initstate(seed, state, sizeof(state));
  75.  
  76.     /*
  77.      * set up the random_tracks array
  78.      */
  79.     for (i = 0; i < cdi.ntracks; i++) {
  80.         for (;;) {
  81.             try = ran(cdi.mintrack, cdi.maxtrack+1);
  82.  
  83.             if (i == 0)
  84.                 break;
  85.  
  86.             for (j = 0; j < i; j++) {
  87.                 if (random_tracks[j] == try)
  88.                     goto again;
  89.             }
  90.  
  91.             /* not a repeat */
  92.             break;
  93.  
  94.             again:;
  95.         }
  96.  
  97.         random_tracks[i] = try;
  98.     }
  99.  
  100.     if (debug == True) {
  101.         debug_printf(1, "shuffle_setup: ");
  102.         for (i = 0; i < cdi.ntracks; i++)
  103.             debug_printf(1, "%d ", random_tracks[i]);
  104.         debug_printf(1, "\n");
  105.     }
  106. }
  107.  
  108. unsigned char
  109. shuffle_next_track() {
  110.     if (cdi.currand == -1)
  111.         return(cdi.mintrack);
  112.  
  113.     if (cdi.currand == cdi.ntracks) {
  114.         fprintf(stderr, "shuffle_get_track: ran off end\n");
  115.         return(cdi.mintrack);
  116.     }
  117.  
  118.     return(random_tracks[cdi.currand++]);
  119. }
  120.  
  121. unsigned char
  122. shuffle_prev_track() {
  123.     if (cdi.currand == -1)
  124.         return(cdi.mintrack);
  125.  
  126.     cdi.currand -= 2;
  127.  
  128.     if (cdi.currand < 0)
  129.         cdi.currand = 0;
  130.  
  131.     return(random_tracks[cdi.currand++]);
  132. }
  133.